package com.xhh.greynewsapp.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.xhh.greynewsapp.MainActivity;
import com.xhh.greynewsapp.R;
import com.xhh.greynewsapp.activity.LoginActivity;
import com.xhh.greynewsapp.model.user.User;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.QueryListener;

/**
 * @description
 * @author: weiXhh
 * @create: 2020-05-26 14:02
 **/
public class MineFragment extends Fragment {


    @BindView(R.id.mine_fragment_login_or_logout)
    Button btnLoginOrLogout;

    @BindView(R.id.mine_fragment_username)
    TextView username;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_mine, container, false);

        ButterKnife.bind(this, view);

        // 加载个人信息
        //getMyInfo();

        btnLoginOrLogout.setOnClickListener(click -> {

            startActivity(new Intent(getActivity(), LoginActivity.class));

        });


        return view;
    }

    private void getMyInfo() {

        BmobUser bu = BmobUser.getCurrentUser(BmobUser.class);
        String id = bu.getObjectId();
        BmobQuery<User> bmobQuery = new BmobQuery<>();
        bmobQuery.getObject(id, new QueryListener<User>() {
            @Override
            public void done(User user, BmobException e) {
                if (e == null) {
                    username.setText(user.getUsername());
                } else {
                    Toast.makeText(getActivity(), "加载失败", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
}
